<style scoped>
  .register{
    height:100%;
    background: #edf1f5;
  }
  .header{
    display: flex;
    flex-direction: column;
  }
  .head{
    height:60px;
    padding-top: 10px;
    display: flex;
    justify-content: center;
    align-items:center;
    border-bottom: 1px solid #E9E9E9;
    box-sizing: border-box;
    background: #ffffff;
    position: relative;
  }
  .head-left{
    position: absolute;
    width:40px;
    height:100%;
    left:0;
    top:0;
    color: #7f7a8e;
    text-align: center;
  }
  .head-left h1{
    margin-top: 22px;
  }
  .head-middle{
    font-size: 16px;
  }
  .head-right{
    position: absolute;
    right:10px;
    top:15px;
  }

  input{
    border:none;
  }
  .content{
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100%;
    margin-top: 80px;
    /*height:100%;*/
  }
  /*.content1>h3{*/
    /*margin-bottom: 20px;*/
  /*}*/

  .input{
    width:100%;
  }
  .input>div{
    margin:0 5%;
  }
  .input>div>.zhmm{
    background: #ffffff;
    border-radius: 10px;
  }
  .input>div>.zhmm>div{
    display: flex;
    align-items: center;
  }
  .input>div>.zhmm>div>label{
    flex: 2;
    display: flex;
    justify-content: center;
    align-items:center;
  }
  .input>div>.zhmm>div>input{
    flex: 7;
    display: flex;
    align-items: center;
    height:80%;
  }
  .input>div>.zhmm>div{
    border-bottom: 1px solid #e9e9e9;
    box-sizing: border-box;
  }
  .input>div>.zhmm>.yanzhengma label{
    /*display: flex;*/
    flex:2;
    /*flex-direction: row;*/
  }
  .input>div>.zhmm>.yanzhengma div{
    flex: 7;
    display: flex;
    height:100%;
    flex-direction: row;
  }
  .input>div>.zhmm>.yanzhengma div input{
    flex: 1;
  }
  .input>div>.zhmm>.yanzhengma div p{
    display: flex;
    justify-content: center;
    align-items: center;
    height:100%;
    background: #4D66FF;
    color: #ffffff;
    border-radius: 4px;
    padding:0 4px;
  }
  .submit{
    margin-top: 30px;
    border-radius: 10px;
    background: #4D66FF;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .submit h3{
    color: #ffffff;
  }
  @media screen and (max-width: 320px) {
    .input>div>.zhmm>div{
      height:43px;
    }
    .submit{
      height:43px;
    }
    .input>div>.zhmm input{
      font-size: 1.2rem;
    }
  }
  @media screen and (max-width:375px) and (min-width:321px) {
    .input>div>.zhmm>div{
      height:50px;
    }
    .submit{
      height:50px;
    }
    .input>div>.zhmm input{
      font-size: 1.4rem;
    }
  }
  @media screen and (min-width: 376px){
    .input>div>.zhmm>div{
      height:60px;
    }
    .submit{
      height:60px;
    }
    .input>div>.zhmm input{
      font-size: 1.6rem;
    }
  }
  .confirm{
    display: flex;
    justify-content: center;
    margin-top: 8px;
  }
  /*验证*/
  .test{
    margin-left: 20px;
    margin-top: 4px;
    color: #e43937;
  }

  .name{
    position: relative;
  }
  .name>p{
    position: absolute;
    right:10px;
    top:14px;
  }
  .password{
    position: relative;
  }
  .password>p{
    position: absolute;
    right:10px;
    top:14px;
  }
</style>

<template>
  <div class="register" ref="nana">
    <div class="head">
      <div class="head-left" @click="goBack"><h1 class="iconfont icon-xiangzuo1"></h1></div>
      <div class="head-middle"><h3>注册账户</h3></div>
    </div>
    <form>
    <div class="content">
      <div class="input">
        <div>
          <div class="zhmm">
            <div class="name">
              <label><p>账户</p></label>
              <input type="tel" placeholder="请输入手机号" v-model="account">
              <p class="iconfont icon-guanbi" @click="clearPhone"></p>
            </div>
            <small class="test" v-if="hasPhone">{{testPhone}}</small>
            <div class="password">
              <label><p>密码</p></label>
              <input type="password" placeholder="请输入登录密码" v-model="pwd1" id="pwd1">
              <p class="iconfont" v-bind:class="{'icon-attention_forbid': isVisible1,'icon-attention_forbid1': isVisible2}" @click="pwdIsVisible1"></p>
            </div>
            <small class="test" v-if="hasPassword">{{testPassword}}</small>
            <div class="password">
              <label><p>确认密码</p></label>
              <input type="password" @keyup.enter="send" placeholder="请再次输入登录密码" v-model="pwd2" id="pwd2">
              <p class="iconfont" v-bind:class="{'icon-attention_forbid': isVisible3,'icon-attention_forbid1': isVisible4}" @click="pwdIsVisible2"></p>
            </div>
            <small class="test" v-if="hasPwd">{{testPwd}}</small>
            <div class="yanzhengma">
              <label><p>验证码</p></label>
              <div>
                <input type="number" @keyup.enter="confirm" placeholder="请输入验证码" v-model="verificationCode">
                <p @click="send">
                  <span v-if="sendMsgDisabled">{{time+'秒后获取'}}</span>
                  <span v-if="!sendMsgDisabled">{{sendCode}}</span>
                </p>
              </div>
            </div>
            <small class="test" v-if="hasCode">{{testCode}}</small>
          </div>
          <div class="submit" @click="confirm">
            <h3>确认注册</h3>
          </div>
          <div class="confirm">
            <p>点击"确认注册",即表示您同意</p><p style="color: #4D66FF;text-decoration: underline">产品使用许可协议</p>
          </div>
        </div>
      </div>
    </div>
    </form>
  </div>
</template>

<script>
  import axios from 'axios'
  import { MessageBox } from 'mint-ui'
  import { Toast } from 'mint-ui'
  import host from '../config.js'

  export default {
    data () {
      return {
        verificationCode: null,
//        用户名
        account: null,
//        密码1
        pwd1: null,
//        密码2
        pwd2: null,
        message: null,
        sendCode: '发送验证码',
        time: 60, // 发送验证码倒计时
        sendMsgDisabled: false,
//        sendMsgDisabled: true
//        验证
//        账号
        hasPhone: false,
        testPhone: "",
//        密码
        hasPassword: false,
        testPassword: null,
        hasPwd: false,
        testPwd: "两次密码不一致",
//        验证码
        hasCode: false,
        testCode: null,
//        密码是否可见
        isVisible1: true,
        isVisible2: false,
        isVisible3: true,
        isVisible4: false,
        isRegister: {}
      }
    },
    watch: {
      account: function (newValue, oldValue) {
        if (newValue !== '' && newValue !== null){
          this.hasPhone = false
        }
      },
      pwd1: function (newValue, oldValue) {
        if (newValue !== '' || newValue !== null){
          this.hasPassword = false
        }
      },
      pwd2: function (newValue, oldValue) {
        if (newValue !== '' || newValue !== null){
          this.hasPwd = false
        }
      },
      verificationCode: function (newValue, oldValue) {
        if (newValue !== '' || newValue !== null){
          this.hasCode = false
        }
      }
    },
    mounted () {
      let h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
      let h1 = h
      this.$refs.nana.style.height = h1 + 'px'
    },
    methods: {
      goBack () {
        this.$router.go(-1)
      },
      send () {
        if(this.sendMsgDisabled === true) {
          return
        }
        let that = this
        let sendData = {
          "phone": that.account,
          "act": "reg"
        }
        if(!(/^1[3|4|5|7|8][0-9]\d{4,8}$/.test(that.account))){
          Toast({
            'message': '不是完整的11位手机号或者正确的手机号前七位',
            duration: 1500
          })
          return;
        }
        function sedMessage () {
          let _that = that
          axios({
            method: 'post',
            url: host.host + "/api/usr/verifyCode",
            data: JSON.stringify(sendData)
          }).then(function (data) {
            if(data.data.code != 200){
              if(data.data.code === 6004){
                that.sendMsgDisabled = false
              }
              Toast({
                message: data.data.msg,
                duration: 1000
              })
              return
            } else if(data.data.code == 200) {
              that.sendMsgDisabled = true
              let interval = window.setInterval(function() {
                if ((that.time--) <= 0) {
                  that.sendMsgDisabled = false
                  that.time = 60
                  window.clearInterval(interval)
                }
              }, 1000)
            }
          })
        }
        if(that.sendMsgDisabled === false){
          sedMessage ()
        }
      },
      confirm () {
        if(this.account === null){
          this.hasPhone = true
          this.testPhone = '账户不能为空'
          return
        }
        if(this.pwd1 === null || this.pwd2 === null){
          this.hasPassword = true
          this.testPassword = '密码不能为空'
          return
        } else if (this.pwd1.trim().length === 0 || this.pwd2.trim().length === 0){
          this.hasPassword = true
          this.testPassword = '密码不能为空'
          return
        }else{
          if(this.pwd1.trim().length > 20 || this.pwd2.trim().length < 6 || this.pwd1.trim().length >= 20 || this.pwd2.trim().length < 6){
            this.hasPassword = true
            this.testPassword = '密码必须为6-20位'
            return
          }
        }
        if(this.pwd1 !== this.pwd2) {
          this.hasPwd = true
          return
        }
        if(this.verificationCode === null){
          this.hasCode = true
          this.testCode = '验证码不能为空'
          return
        }
        let send = {
          "phone": this.account,
          "passwd": this.pwd1,
          "code": this.verificationCode
        }
        let that = this
        axios({
          method: 'post',
          url: host.host + "/api/usr/reg",
          data: JSON.stringify(send)
        }).then(function (data) {
          if(data.data.code === 200) {
            Toast({
              message: '注册成功',
              duration: 1000
            })
            let interval = window.setTimeout(function() {
              that.$router.push({'path':'/login'})
            }, 1000)
          }else{
            Toast({
              message: data.data.msg,
              duration: 1000
            })
          }
        })
      },
      clearPhone () {
        this.account = null
      },
      pwdIsVisible1 () {
        this.isVisible1 = !this.isVisible1
        this.isVisible2 = !this.isVisible2
        console.log(document.getElementById('pwd1').getAttribute('type'))
        if(document.getElementById('pwd1').getAttribute('type') === 'password'){
          document.getElementById('pwd1').setAttribute('type','text')
        }else{
          document.getElementById('pwd1').setAttribute('type','password')
        }
      },
      pwdIsVisible2 () {
        this.isVisible3 = !this.isVisible3
        this.isVisible4 = !this.isVisible4
        console.log(document.getElementById('pwd2').getAttribute('type'))
        if(document.getElementById('pwd2').getAttribute('type') === 'password'){
          document.getElementById('pwd2').setAttribute('type','text')
        }else{
          document.getElementById('pwd2').setAttribute('type','password')
        }
      }
    }
  }
</script>
